//
//  HQMainViewController.m
//  wbOC
//
//  Created by sheji on 16/3/1.
//  Copyright (c) 2016年 sheji. All rights reserved.
//

#import "HQMainViewController.h"
#import "HQHomeViewController.h"
#import "HQMessageViewController.h"
#import "HQDiscoverViewController.h"
#import "HQProfileViewController.h"
#import "HQNavigationController.h"

@interface HQMainViewController ()

/**
 *  添加撰写按钮
 */
@property (nonatomic, strong)UIButton *composeButton;
@end

@implementation HQMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self addChildViewControllers];
    
}

//在布局变化之前对子空间进行布局
-(void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    [self setupComposeButton];
    
    //隐藏tabbar上方分割线
    self.tabBar.shadowImage = [[UIImage alloc]init];
    self.tabBar.backgroundImage = [UIImage imageNamed:@"tabbar_background"];
}

#pragma mark - 设置撰写按钮的位置
-(void)setupComposeButton
{
    CGRect rect = self.tabBar.bounds;
    CGFloat w = rect.size.width / self.childViewControllers.count - 1;
    
    self.composeButton.frame = CGRectInset(rect, 2 * w, 0);
}
/**
 *  撰写按钮的点击事件
 */
-(void)clickComposeButton
{
    NSLog(@"点击撰写按钮");
}
#pragma mark - 设置子控制器
-(void)addChildViewControllers
{
    //设置tabbar的TintColor
    self.tabBar.tintColor = [UIColor orangeColor];
    
    //添加子控制器
    [self addChildViewController:[[HQHomeViewController alloc] init] Title:@"首页" imageName:@"tabbar_home"];
    [self addChildViewController:[[HQMessageViewController alloc] init] Title:@"消息" imageName:@"tabbar_message_center"];
    
    //添加一个空白控制器
    [self addChildViewController:[[UIViewController alloc] init]];
    
    [self addChildViewController:[[HQDiscoverViewController alloc] init] Title:@"发现" imageName:@"tabbar_discover"];
    [self addChildViewController:[[HQProfileViewController alloc] init] Title:@"我" imageName:@"tabbar_profile"];
}
/**
 *  添加子控制器
 *
 *  @param VC        子控制器
 *  @param title     标题
 *  @param imageName 图片名字
 */
-(void)addChildViewController:(UIViewController *)VC Title:(NSString *)title imageName:(NSString *)imageName
{
    VC.title = title;
    VC.tabBarItem.image = [UIImage imageNamed:imageName];
    //选中图片
    NSString *selectImageName = [NSString stringWithFormat:@"%@_selected",imageName];
    VC.tabBarItem.selectedImage = [[UIImage imageNamed:selectImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    //添加根控制器
     HQNavigationController *nav = [[HQNavigationController alloc]initWithRootViewController:VC];
    [self addChildViewController:nav];
}

#pragma mark - 懒加载控件
-(UIButton *)composeButton
{
    if (_composeButton == nil) {
        _composeButton = [[UIButton alloc] init];
        
        //设置按钮的图片
        [_composeButton setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
        [_composeButton setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
        [_composeButton setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
        [_composeButton setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
        //把按钮添加到tabbar
        [self.tabBar addSubview:_composeButton];
        
        //按钮的监听方法
        [_composeButton addTarget:self action:@selector(clickComposeButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _composeButton;
}
@end
